﻿#ifndef CONTROLTIMER_H
#define CONTROLTIMER_H

#include <QObject>
#include <QTimer>
#include <QMap>
#include <QMutex>
#include <QDebug>
#include <QShortcut>
#include <QPushButton>
#include "Directionalpad.h"

namespace RO_Robot{
#define M_Key "M_key"

class ControlTimer : public QObject
{
    Q_OBJECT
public:
    explicit ControlTimer(QObject *parent = nullptr);
    ~ControlTimer();
    enum ControlType{
        E_Keyboard, //键盘控制机器人.
        E_Button //鼠标点击界面上按钮控制.
    };
    void releaseKey(const QKeySequence &keyS);
    void stopAllTimer();
    qreal maxSpeed() const;
    void setMaxSpeed(const qreal &maxSpeed);

    qreal maxRot() const;
    void setMaxRot(const qreal &maxRot);

    void autoStopTimer();
    void emergencyStop();
    void startMove(const QString &key, bool autoStop);

    void releaseKey(const QString &key, const QString &fromClassName);

    void stopMoving(int type);
    void setMoving(int type, qreal vx, qreal vy, qreal w, qreal steer);
    void setKeyTimer(const QString &key, QPushButton *&btn);
    void setKeyTimer(DirectionalPad *&directionalpad,QPainterPath *&btnArea,const QString &Key);

    QMap<QString, QPushButton *> buttonMap() const;
    QMap<QString,QPainterPath *> buttonArea() const;
    void setButtonMap(const QMap<QString,QPainterPath*> &value);
    void setButtonMap(const QMap<QString, QPushButton *> &value);

    void setButtonChecked(const QString &key, bool checked);
    int chassisMode() const;
    void setChassisMode(int mode);

    void modeFuntion(QTimer *timer);
    void steerModeFuntion(QTimer*timer);
    int getSteerValueAngleH() const;
    void setSteerValueAngleH(int steerValueAngleH);

    bool isHasTimerRunning();
    void attenuationSpeedStop();

    void setCompatibilityMode(bool isCompatibilityMode);
    bool isCompatibilityMode() const;
    void clearAllBtnState();

    void clearAllBtnAreaState();
    ControlType controlType() const;
    void setControlType(ControlType newControlType);
protected:
    void keyboardEvent();
    void startKeyboardThread();
signals:
    void sigControlSpeed(int,qreal,qreal,qreal,qreal);
    void sigControlStateChanged(bool isControl);

    void sigKeyChecked(const QString &key);
    void sigKeyUnChecked(const QString &key);
public slots:
    void slotTimeOut();
    void slotShortcut();
    void slotAutoStopTimeOut();
    void slotButtonPrssed();
    void slotButtonReleased();
    void slotControlParameter(qreal dMaxSpeed, qreal dMaxSpeedAcc, qreal dMaxRot, qreal dMaxRotAcc);
    void slotZeroAngle();

    void slotButtonAreaPressed(QString key);
    void slotButtonAreaReleased(QString key);
private:
    //每个快捷键都绑定一个定时器
    QMap<QString,QTimer*>_timerMap;
    //按钮
    QMap<QString,QPushButton*>_buttonMap;
    QMap<QString,QPainterPath*>_buttonArea; //画出的区域

    QMutex _controlSendMutex;
    qreal _vxSpeed = 0;
    qreal _vySpeed = 0;
    qreal _wSpeed = 0;

    qreal _maxSpeed = 0.5;///前后直线最大速度
    qreal _maxRot = 30;///转弯最大速度

    //机器人模型文件:mode 4:单舵轮 3:omni 5:双舵轮(支持横移)
    int  _chassisMode = 0;
    int _steerValueAngleH = 0;//舵轮0停止 1左++  -1右++  -2归零

    int _timeOutMs {200};
    //兼容模式
    bool _isCompatibilityMode = false;
    //true:正在被控制.   false:未被控制.
    bool _isControlState {false};
    //默认键盘控制.
    ControlType _controlType {E_Keyboard};
    bool _isRelease = false;
    QTimer *_dogTimer {Q_NULLPTR};

    QPainter myPainter;
};
}//namespace
#endif // CONTROLTIMER_H
